package com.itheima.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.Product;
import com.itheima.factory.DaoInstanceFactory;
import com.itheima.mapper.store.ProductDao;
import com.itheima.service.store.ProductService;

import java.util.List;
import java.util.UUID;

public class ProductServiceImpl implements ProductService {
    @Override
    public void save(Product product) {
        ProductDao productDao = DaoInstanceFactory.getMapper(ProductDao.class);
        product.setId(UUID.randomUUID().toString());
        productDao.save(product);
    }


    @Override
    public void update(Product product) {
        ProductDao productDao = DaoInstanceFactory.getMapper(ProductDao.class);
        productDao.update(product);
    }

    @Override
    public Product findById(String id) {
        ProductDao productDao = DaoInstanceFactory.getMapper(ProductDao.class);
        return productDao.findById(id);
    }

    @Override
    public List<Product> findAll() {
        ProductDao productDao = DaoInstanceFactory.getMapper(ProductDao.class);
        return productDao.findAll();
    }

    @Override
    public PageInfo findAll(int page, int size) {
        ProductDao productDao = DaoInstanceFactory.getMapper(ProductDao.class);
        PageHelper.startPage(page,size);
        List<Product> all = productDao.findAll();
        PageInfo pageInfo = new PageInfo(all);
        return pageInfo;
    }

    @Override
    public void deleteByIds(String[] ids) {
        ProductDao productDao = DaoInstanceFactory.getMapper(ProductDao.class);
        for (String id : ids) {
            ProductDao productDao1 = DaoInstanceFactory.getMapper(ProductDao.class);
            productDao1.delete(id);
        }
    }

    @Override
    public void delete(String id) {
        ProductDao productDao = DaoInstanceFactory.getMapper(ProductDao.class);
        productDao.delete(id);
    }

    @Override
    public void close(String[] arr) {
        ProductDao productDao = DaoInstanceFactory.getMapper(ProductDao.class);
        for(String id:arr){
            productDao.close(id);
        }
    }

    @Override
    public void open(String[] arr) {
        ProductDao productDao = DaoInstanceFactory.getMapper(ProductDao.class);
        for(String id:arr){
            productDao.open(id);
        }
    }

    @Override
    public PageInfo findByName(String pName, int currentPage, int pageSize) {
        ProductDao productDao = DaoInstanceFactory.getMapper(ProductDao.class);
        PageHelper.startPage(currentPage,pageSize);
        List<Product> products =productDao.findByName(pName);

        return new PageInfo(products);
    }
}
